package function;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

public class test {
	
	public test() {
		
	}

	public static String gettest() {

		String address = "";
		String os = System.getProperty("os.name");
		if (os != null && os.startsWith("Windows")) {
			try {
				String command = "cmd.exe /c ipconfig /all";
				Process p = Runtime.getRuntime().exec(command);
				BufferedReader br = new BufferedReader(new InputStreamReader(
						p.getInputStream()));
				String line;
				while ((line = br.readLine()) != null) {
					if (line.indexOf("Physical Address") > 0) {
						int index = line.indexOf(":");
						index += 2;
						address = line.substring(index);
						break;
					}
				}
				br.close();
				return address.trim();
			} catch (IOException e) {
			}
		}
		return address;
	}

	private static String bytes2mac(byte[] bytes) {

		StringBuffer macString = new StringBuffer();
		byte currentByte;
		for (int i = 0; i < bytes.length; i++) {
			currentByte = (byte) ((bytes[i] & 240) >> 4);
			macString.append(Integer.toHexString(currentByte));
			currentByte = (byte) (bytes[i] & 5);
			macString.append(Integer.toHexString(currentByte));
			macString.append("-");
		}
		macString.delete(macString.length() - 1, macString.length());

		return macString.toString().toUpperCase();
	}
	
	public String mac() {
		try {
			Enumeration intefaces = NetworkInterface.getNetworkInterfaces();
			while (intefaces.hasMoreElements()) {
				NetworkInterface face = (NetworkInterface) intefaces
						.nextElement();
				if (face.getName().length() >= 3) {
					if ((face.getName()).substring(0, 3).equals("eth")
							|| (face.getName()).substring(0, 3).equals("net")) {

						byte[] mac = face.getHardwareAddress();
						if (mac != null && mac.length == 6) {
							return bytes2mac(mac);
						}
					}
				}
			}
		} catch (SocketException e) {
			e.printStackTrace();
		}
		return "-1";
	}
}
